package com.sw.servlet;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author sw
 * @project minicat
 * @description 请求信息 封装为Request
 * @date 2020/4/29 22:05
 */
public class Request {
    private String url;
    /**
     * 请求方式get 或post
     */
    private String method;
    private InputStream inputStream;
    /**
     * 输入流
     * @param inputStream
     */
    public Request(InputStream inputStream) throws IOException {
        this.inputStream=inputStream;
            //从输入流获取请求信息 IO模型 BIO
            int count=0;
            while (count==0){//网络请求可能为空
                count=inputStream.available();
            }
            byte[] bytes=new byte[count];
            inputStream.read(bytes);
            String inputStr=new String(bytes);
            //获取第一行请求头信息
            String firstLineStr=inputStr.split("\\n")[0];
            String [] strings=firstLineStr.split( " ");
            this.method=strings[0];
            this.url=strings[1];
            //System.out.println("____请求信息:\n"+new String(bytes));
            //System.out.println("method "+method+",url "+url);

    }

    public Request() {
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}

